package com.chuji.crmcj.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.bean.GoodMan;

import java.util.ArrayList;

public class NameAdapter extends BaseAdapter implements ListAdapter {

    private ArrayList<GoodMan> mPersons;
    private Context mContext;
    private String mFirstPinyin;
    private String mPreFirstPinyin;

    public NameAdapter(Context context, ArrayList<GoodMan> persons) {
        mPersons = persons;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mPersons.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item, null);
            holder = new ViewHolder();
            holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tvPinyin = (TextView) convertView.findViewById(R.id.tv_pinyin);
            holder.tvLeastTime=(TextView)convertView.findViewById(R.id.tv_least_time);

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.tvLeastTime.getLayoutParams();
            params.rightMargin=100;
            holder.tvLeastTime.setLayoutParams(params);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GoodMan goodMan = mPersons.get(position);
        mFirstPinyin = String.valueOf(goodMan.mPinyin.charAt(0));
        if (position == 0) {
            mPreFirstPinyin = "-";
        } else {
            mPreFirstPinyin = String.valueOf(mPersons.get(position - 1).mPinyin.charAt(0));
        }
        holder.tvName.setText(goodMan.mName);
        holder.tvPinyin.setVisibility(TextUtils.equals(mPreFirstPinyin, mFirstPinyin) ? View.GONE
                : View.VISIBLE);
        holder.tvPinyin.setText(String.valueOf(goodMan.mPinyin.charAt(0)));
        return convertView;
    }

    static class ViewHolder {
        TextView tvName;
        TextView tvPinyin;
        TextView tvLeastTime;
    }

}
